<?php
namespace app\api\controller\album;
use app\common\controller\Api;
use think\Db;
class Catalog extends Api{

    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    protected function _initialize()
    {
        parent::_initialize();

    }
    public function index(){
        $id = $this->auth->id;
        $list = Db::name('AlbumCatalog')->where('user_id',$id)->field('id,title,description,createtime,updatetime,is_default')->order('is_default desc')->select();
        if(!empty($list)){
            foreach ($list as $key => &$value) {
               $images = Db::name('AlbumPhoto')->where('album_catalog_id',$value['id'])->order('weigh esc')->select();
               if(!empty($images)){
                foreach($images as $k =>&$v){
                    $v['thumb'] = cdnurl($v['image'])."?x-oss-process=image/resize,w_150";
                    $v['image'] = cdnurl($v['image'])."?x-oss-process=image/resize,w_750,h_750,m_mfit/quality,q_80";
                    
                }
                $value['images'] = $images;
               }else{
                $value['images'] = [];
               }
               $value['image'] = cdnurl('/xiangce/uploads/20230522/117ace66304ae3d6e859a890a1266d87.png')."?x-oss-process=image/resize,w_750";
               $value['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
            }
        }else{
            $workItem = \app\admin\model\AlbumCatalog::create([
            'title'=>'系统相册',
            'description'=>'系统相册',
            'user_id'=>$id,
            'is_default'=>1
            ],true);
            $list = Db::name('AlbumCatalog')->where('user_id',$id)->field('id,title,description,createtime,updatetime,is_default')->order('is_default desc')->select();
            foreach ($list as $key => &$value) {
                $value['images'] = [];
                $value['image'] = cdnurl('/xiangce/uploads/20230522/117ace66304ae3d6e859a890a1266d87.png')."?x-oss-process=image/resize,w_750";
                $value['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
             }
        }
        $this->success('相册', $list);
    }

    public function photo(){
        $url = $this->request->param('url');
        $album_id = $this->request->param('catalog_id');
        $count = Db::name('AlbumPhoto')->where('album_catalog_id',$album_id)->max('weigh');
            $albumphoto = \app\admin\model\AlbumPhoto::create([
                'album_catalog_id'=>$album_id,
                'image'=>str_ireplace('https://oss.bigchun.com','',$url),
                'weigh'=>$count?($count+1):1,
            ],true);
            $albumphoto->image = $url."?x-oss-process=image/resize,w_750,h_750,m_mfit/quality,q_80";
            $albumphoto->thumb = $url."?x-oss-process=image/resize,w_150";
        $this->success('相册上传图片', $albumphoto);
    }
    public function detail(){
        $id = $this->request->post('id');
        $info = Db::name('AlbumCatalog')->where('id',$id)->field('id,title,description,is_default,createtime,updatetime')->find();
        if(!empty($info)){
               $images = Db::name('AlbumPhoto')->where('album_catalog_id',$info['id'])->order('weigh esc')->select();
               if(!empty($images)){
                foreach($images as $k =>&$v){
                    $v['thumb'] = cdnurl($v['image'])."?x-oss-process=image/resize,w_150";
                    $v['image'] = cdnurl($v['image'])."?x-oss-process=image/resize,w_750,h_750,m_mfit/quality,q_80";
                    
                }
                $info['images'] = $images;
               }else{
                $info['images'] = [];
               }
               $info['image'] = cdnurl('/xiangce/uploads/20230522/117ace66304ae3d6e859a890a1266d87.png')."?x-oss-process=image/resize,w_750,h_750,m_mfit/quality,q_80";
               $info['createtime'] = date('Y-m-d H:i:s',$info['createtime']);
        }else{
            $info = [];
        }
        $this->success('相册详情', $info);
    }
    public function add(){
        $userid = $this->auth->id;
        $title = $this->request->post('title');
        $description = $this->request->post('description','');
        if(empty($title)){
            $this->error('标题不能为空');
        }
        if(empty($description)){
            $description = $title;
        }
        $albumCatalog = \app\admin\model\AlbumCatalog::create([
            'title'=>$title,
            'description'=>$description,
            'image'=>'/xiangce/uploads/20230522/117ace66304ae3d6e859a890a1266d87.png',
            'user_id'=>$userid
        ],true);
        $this->success('相册创建成功', $albumCatalog);
    }

    public function del(){
        $userid = $this->auth->id;
        $id = $this->request->post('id');
        Db::name('AlbumPhoto')->where('album_catalog_id',$id)->delete();
        Db::name('AlbumCatalog')->where('id',$id)->delete();
        $this->success('相册删除成功');
    }
}

?>